package com.tenement.service.impl;

import java.util.ArrayList;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONObject;
import com.tenement.mybatis.dao.HouseMapper;
import com.tenement.mybatis.dao.PhotoMapper;
import com.tenement.mybatis.entity.House;
import com.tenement.mybatis.entity.Photo;
import com.tenement.service.interfaces.IHouseService;
import com.tenement.utils.page.PageUtil;

@Service("IHouseService")
public class HouseServiceImpl implements IHouseService {
	private static final Log logger = LogFactory.getLog(HouseServiceImpl.class);


	@Autowired
	private HouseMapper houseMapper;

	@Autowired
	private PhotoMapper photoMapper;

	/**
	 * 主页模糊查询
	 */
	@Override
	public JSONObject getHouseByLike(String condition) {
		JSONObject json = new JSONObject();
		json.put("houses", houseMapper.getHouseByLike(condition));
		int totalRecord = houseMapper.getHouseCountByLike(condition);
		json.put("totalRecord", totalRecord);
		json.put("pageCount", totalRecord);// 总页数
		json.put("pageIndex", 1);
		json.put("totalRecord", totalRecord);
		// 获取每个房源得图片
		List<House> lh = houseMapper.getHouseByLike(condition);
		List<Photo> lp = new ArrayList<Photo>();
		for (House h : lh) {
			lp.add(photoMapper.getHousePhotoOnlyOne(h.gethPhotoid()));
		}
		json.put("photos", lp);
		return json;
	}

	/**
	 * 获取所有房源信息
	 */
	@Override
	public List<House> getHouseNoReview() {
		return houseMapper.getHouseNoReview();
	}

	@Override
	public int getHouseCount() {
		return 0;
	}

	/**
	 * 7个条件查询
	 */
	@Override
	public JSONObject queryHouseBy7(House house, String sort, String pageIndex, String condition) {

		PageUtil pageUtil = new PageUtil();
		pageUtil.setPageSize(5);// 设置每页显示的记录数
		pageUtil.setPageIndex(Integer.parseInt(pageIndex));// 设置当前页码
		// 获得开始查询的记录数 limit a,b从当前记录数开始查询b条数据
		int startRecord = pageUtil.getStartRecord();
		int totalRecord = houseMapper.getHouseCount(house, condition);
		// 获得总记录数，同时设置总页数，在该方法中
		pageUtil.setTotalrecord(totalRecord);
		List<House> lh = houseMapper.getHouseBy7(house, sort, startRecord, condition);
		JSONObject json = new JSONObject();
		json.put("houses", lh);
		json.put("totalRecord", totalRecord);
		json.put("pageCount", pageUtil.getPageCount());// 总页数
		json.put("pageIndex", pageIndex);
		json.put("totalRecord", totalRecord);

		// 获取每个房源得图片
		List<Photo> lp = new ArrayList<Photo>();
		for (House h : lh) {
			lp.add(photoMapper.getHousePhotoOnlyOne(h.gethPhotoid()));
		}
		json.put("photos", lp);
		return json;
	}

	/**
	 * 信息查询
	 */
	@Override
	public boolean publishHouseMsg(House house) {
		int i = houseMapper.publishHouseMsg(house);
		if (i > 0)
			return true;
		else
			return false;
	}

	/**
	 * 获取房屋详细信息
	 */
	@Override
	public House queryHouseDetail(String hPhotoid) {
		return houseMapper.getHouseDetail(hPhotoid);
	}

	/**
	 * 审核
	 */
	@Override
	public boolean modifyHReviewStatus(House house) {
		int b = houseMapper.modifyHReviewStatus(house);
		logger.info("房源信息审核"+b+house);
		if (b > 0) {
			return true;
		} else {
			return false;
		}

	}
	
	/**
	 * 
	 * @param message
	 * @param u_id
	 */
	public void sendReviewMessage(String message,int u_id){
		
	}

}
